import pygame
from pygame.locals import *

class Character(pygame.sprite.Sprite):
  def __init__(self,images):
    pygame.sprite.Sprite.__init__(self) #call Sprite intializer
    self.image_frames = images
    self.current_frame = 0
    self.image = self.image_frames["south"][self.current_frame]
    self.rect = self.image.get_rect()
    self.rect.topleft = (0,0)
    self.collision_rect = self.rect.inflate(0,-15)
    self.x_velocity = 0
    self.y_velocity = 0
    self.move_cooldown = 0
    self.direction = "south"
    self.speed = 4
    
      
  def handle_event(self,event):
    if event.type == KEYDOWN:
      if event.key == K_LEFT:
          self.x_velocity -= self.speed
      elif event.key == K_RIGHT:
          self.x_velocity += self.speed
      elif event.key == K_UP:
          self.y_velocity -= self.speed
      elif event.key == K_DOWN:
          self.y_velocity += self.speed        
    if event.type == KEYUP:
      if event.key == K_LEFT:
          self.x_velocity += self.speed
      elif event.key == K_RIGHT:
          self.x_velocity -= self.speed
      elif event.key == K_UP:
          self.y_velocity += self.speed
      elif event.key == K_DOWN:
          self.y_velocity -= self.speed
    if event.type == JOYAXISMOTION:
      if event.axis == 1:
        self.y_velocity = event.value * self.speed
      elif event.axis == 0:
        self.x_velocity = event.value * self.speed
    
    # make sure they don't break the speed barrier
    if self.x_velocity > self.speed:
      self.x_velocity = self.speed
    if self.x_velocity < -self.speed:
      self.x_velocity = -self.speed
    if self.y_velocity > self.speed:
      self.y_velocity = self.speed
    if self.y_velocity < -self.speed:
      self.y_velocity = -self.speed
    
  def get_direction(self):
    direction = self.direction
    if self.y_velocity > 0:
      direction = "south"
    if self.y_velocity < 0:
      direction = "north"
    if self.x_velocity > 0:
      direction = "east"
    if self.x_velocity < 0:
      direction = "west"
    self.direction = direction
    return direction
  
  def get_scroll(self,viewport=(640,480)):
    max_x = viewport[0]
    max_y = viewport[1]
    x = self.rect.center[0] - max_x/2
    y = self.rect.center[1] - max_y/2
    if x < 0:
      x = 0
    if x > max_x:
      x = max_x
    if y < 0:
      y = 0
    if y > max_y:
      y = max_y
    return (x,y)
  
  def update(self,mapsize=(1280,960)):
    if self.move_cooldown == 0:
      self.move_cooldown = 6
      
      # change image
      if self.x_velocity != 0 or self.y_velocity != 0:
        if self.current_frame == 0:
          self.current_frame = 1
        else:
          self.current_frame = 0
    else:
      self.move_cooldown -= 1
      
    old_left  = self.rect.left
    old_top   = self.rect.top
    
    self.image = self.image_frames[self.get_direction()][self.current_frame]
    
    self.rect.move_ip((self.x_velocity, self.y_velocity))
    self.collision_rect = self.rect.inflate(0,-15)
    self.collision_rect.top = self.rect.top + 15
    
#     intersections = [sprite for sprite in tile_sprites if sprite.rect.colliderect(self.collision_rect)]
#     
#     for i in intersections:
#       if i.index > 0 and i.index < 4: # lame thing to do for now...
#         if i.rect.left > self.collision_rect.left or \
#         self.collision_rect.left < i.rect.left + Arena.tilesize:
#           self.rect.left = old_left
#         if self.collision_rect.top + Arena.tilesize > i.rect.top or \
#         self.collision_rect.top < i.rect.top + Arena.tilesize:
#           self.rect.top  = old_top
      
    if self.rect.left < 0:
        self.rect.left = 0
    elif self.rect.right > mapsize[0]:
        self.rect.right = mapsize[0]
        
    if self.rect.top <= 0:
        self.rect.top = 0
    elif self.rect.bottom >= mapsize[1]:
        self.rect.bottom = mapsize[1]
